#pragma once
#include <unordered_map>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>

const std::string default_path = "./resource/dict.txt";

class Translate
{
public:
    Translate(const std::string& path = default_path)
        :_dict_path(path)
    {
        SepFile();
        Init();
    }
    void SepFile()
    {
        std::ifstream in(_dict_path);

        std::string tmp;
        while(std::getline(in, tmp))
            _lines.push_back(tmp);
    }
    void Init()
    {
        for(auto& str : _lines)
        {
            int index = 0;
            if((index = str.find(' ', 0)) == std::string::npos)   
                continue; 
            _dict[str.substr(0, index)] = str.substr(index + 1);         
        }
    }
    void Debug()
    {
        for(auto& str : _lines)
            std::cout << str << std::endl;
        
        for(auto& pair : _dict)
            std::cout << pair.first << " : " << pair.second << std::endl;
    }
    std::string GetChinese(const std::string& word)
    {
        if(_dict.count(word))
            return _dict[word];
        else return "不在词典中";
    }
private:
    std::string _dict_path;
    std::unordered_map<std::string, std::string> _dict;
    std::vector<std::string> _lines;
};